//
//  NewsDetailView.m
//  Product_NEWS
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 FCH. All rights reserved.
//

#import "NewsDetailView.h"
#import "NightModeHandle.h"
@implementation NewsDetailView
- (void)dealloc {
    [_toolBar release];
    [_btnSave release];
    [_btnShare release];
    [_btnComment release];
    [_webView release];
    [_date release];
    [_source release];
    [_title release];
    [_model release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.title];
        [self addSubview:self.webView];
        [self addSubview:self.source];
        [self addSubview:self.date];
        [self addSubview:self.toolBar];
        [self configureLine];
        
        
    }
    return self;
}


#pragma mark - method
+ (CGFloat)getTextWidth:(NSString *)text {
    return  [text boundingRectWithSize:CGSizeMake(375 * SCREEN_WIDTH, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin |
             NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20*SCREEN_WIDTH]} context:nil].size.width;
}



#pragma mark - lazy loading
- (UILabel *)title {
    if (!_title) {
        self.title = [[[titleLabel alloc] initWithFrame:CGRectMake(10 * SCREEN_WIDTH, 0*SCREEN_HEIGHT, 375* SCREEN_WIDTH, 20*SCREEN_HEIGHT)] autorelease];
        self.title.numberOfLines = 0;
        
        self.title.font = [UIFont systemFontOfSize:16*SCREEN_WIDTH weight:3*SCREEN_WIDTH];
        self.title.lineBreakMode = NSLineBreakByCharWrapping;
    }
    return [[_title retain] autorelease];
}

- (UILabel *)source {
    if (!_source) {
        self.source = [[[WordTite alloc] initWithFrame:CGRectMake(15* SCREEN_WIDTH, 30*SCREEN_HEIGHT, 80* SCREEN_WIDTH, 15*SCREEN_HEIGHT)] autorelease];
        self.source.font = [UIFont systemFontOfSize:14*SCREEN_WIDTH];
        self.source.textColor = [NightModeHandle shareNightModeHandle].wordColor;
    }
    return [[_source retain] autorelease];
}

- (UILabel *)date {
    if (!_date) {
        self.date = [[[WordTite alloc] initWithFrame:CGRectMake(95* SCREEN_WIDTH, 30*SCREEN_HEIGHT, 200* SCREEN_WIDTH, 15*SCREEN_HEIGHT)] autorelease];
        self.date.font = [UIFont systemFontOfSize:14*SCREEN_WIDTH];
        self.date.textColor = [NightModeHandle shareNightModeHandle].wordColor;
        
    }
    return [[_date retain] autorelease];
}
- (UIWebView *)webView {
    if (!_webView) {
        self.webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 50*SCREEN_HEIGHT, 375  * SCREEN_WIDTH, [UIScreen mainScreen].bounds.size.height - 160 *SCREEN_HEIGHT)] autorelease];
        _webView.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
        //默认使NO。这个值决定了用内嵌HTML5播放视频还是用本地的全屏控制。
     //   为了内嵌视频播放，不仅仅需要在这个页面上设置这个属性，还必须的是在HTML中的video元素必须包含webkit-playsinline属性。
        _webView.allowsInlineMediaPlayback = YES;
        _webView.mediaPlaybackRequiresUserAction = NO;
        
    }
    return [[_webView retain] autorelease];
}


- (UIButton *)btnShare {
    if (!_btnShare) {
        self.btnShare = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnShare.frame = CGRectMake(315* SCREEN_WIDTH, 3*SCREEN_HEIGHT, 20* SCREEN_WIDTH, 20*SCREEN_HEIGHT);
        [_btnShare setImage:[UIImage imageNamed:@"分享"] forState:UIControlStateNormal];
    }
    return [[_btnShare retain] autorelease];
}
- (UIButton *)btnSave {
    if (!_btnSave) {
        self.btnSave = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnSave.frame = CGRectMake(252* SCREEN_WIDTH, 0, 23* SCREEN_WIDTH, 23*SCREEN_HEIGHT);
        [_btnSave setImage:[UIImage imageNamed:@"收藏@2x"] forState:UIControlStateNormal];
    }
    return [[_btnSave retain] autorelease];
}

- (UIButton *)btnComment {
    if (!_btnComment) {
        self.btnComment = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnComment.frame = CGRectMake((355 - 160)* SCREEN_WIDTH, 3*SCREEN_HEIGHT, 20* SCREEN_WIDTH, 20*SCREEN_HEIGHT);
        [_btnComment setImage:[UIImage imageNamed:@"评论"] forState:UIControlStateNormal];
    }
    return [[_btnComment retain] autorelease];
}
- (UIView *)toolBar {
    if (!_toolBar) {
        self.toolBar = [[[UIView alloc] initWithFrame:CGRectMake(0, self.webView.frame.size.height + self.webView.frame.origin.y , 375* SCREEN_WIDTH, 44*SCREEN_HEIGHT)] autorelease];
      self.toolBar.backgroundColor = [UIColor colorWithWhite:0.956 alpha:0.800];
        [self.toolBar  addSubview:self.btnSave];
        UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake((375 - 23 - 100)* SCREEN_WIDTH, 23*SCREEN_HEIGHT, 20* SCREEN_WIDTH, 20*SCREEN_HEIGHT)];
        lbl1.text = @"收藏";
        lbl1.textColor = [NightModeHandle shareNightModeHandle].titleColor;
        lbl1.font = [UIFont systemFontOfSize:10*SCREEN_WIDTH];
        [self.toolBar addSubview:lbl1];
        [lbl1 release];
        
        [self.toolBar  addSubview:self.btnShare];
        UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(315* SCREEN_WIDTH, 23*SCREEN_HEIGHT, 20* SCREEN_WIDTH, 20*SCREEN_HEIGHT)];
        lbl2.text = @"分享";
        lbl2.font = [UIFont systemFontOfSize:10*SCREEN_WIDTH];
           lbl2.textColor = [NightModeHandle shareNightModeHandle].titleColor;
        [self.toolBar addSubview:lbl2];
        [lbl2 release];
        
        [self.toolBar addSubview:self.btnComment];
        UILabel *lbl3 = [[UILabel alloc] initWithFrame:CGRectMake((375 - 20 - 160)* SCREEN_WIDTH, 23*SCREEN_HEIGHT, 20* SCREEN_WIDTH, 20*SCREEN_HEIGHT)];
        lbl3.text = @"评论";
        lbl3.font = [UIFont systemFontOfSize:10*SCREEN_WIDTH];
           lbl3.textColor = [NightModeHandle shareNightModeHandle].titleColor;
        [self.toolBar addSubview:lbl3];
        [lbl3 release];
    }
    return [[_toolBar retain] autorelease];
}

#pragma mark - setter 方法
- (void)configureLine {
    //标题分割线
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(5* SCREEN_WIDTH, self.source.frame.origin.y + self.source.frame.size.height, 375* SCREEN_WIDTH, SCREEN_HEIGHT)];
    line.backgroundColor = [UIColor grayColor];
    [self addSubview:line];
    [line release];
}
-(void)setModel:(NewsDetailModel *)model{

    if (_model != model) {
        [_model release];
        _model = [model retain];
        self.title.text = model.title;
        self.date.text = model.ptime;
        self.source.text = model.source;
        //获取图片集信息
        for (int i = 0; i < model.img.count; i++) {
            NSArray *pixArr = [model.img[i][@"pixel"]  componentsSeparatedByString:@"*"];
            if (!pixArr) {
                pixArr = @[[NSString stringWithFormat:@"%f", 355 * SCREEN_WIDTH], [NSString stringWithFormat:@"%f", 355* SCREEN_WIDTH * 2.0 / 3]];
            }
            if ([pixArr[0] floatValue] > 375 * SCREEN_WIDTH) {
                pixArr = @[[NSString stringWithFormat:@"%f", 355* SCREEN_WIDTH],[NSString stringWithFormat:@"%f",[pixArr[1] floatValue]  * 375* SCREEN_WIDTH / [pixArr[0] floatValue]]];
                
            }
            NSString *imgTag = [NSString stringWithFormat:@"<div style=\"border:1px font-size:14; text-align:center; solid #ccc;\"> <img style=\" width:%@; height:%@;\" src=\"%@\"><span>%@</span></div>",pixArr[0], pixArr[1], model.img[i][@"src"], model.img[i][@"alt"]];
            model.body = [model.body stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"<!--IMG#%d-->", i] withString:imgTag];
        }
        for (int i = 0; i < model.video.count; i++) {
            NSString *videoTag = [NSString stringWithFormat:@"<div style=\"border:1px font-size:14; text-align:center; solid #ccc;\"><video id=\"player\" width=\"300\" height=\"250\" controls webkit-playsinline controls='controls' autoplay='autoplay'> <source src=\"%@\" type=\"video/mp4\" /> </video></div>", model.video[i][@"url_mp4"]];
            model.body = [model.body stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"<!--VIDEO#%d-->", i] withString:videoTag];
        }
        [self.webView loadHTMLString:model.body baseURL:nil];
        
        CGRect frame = self.source.frame;
        frame.size.width = [NewsDetailView getTextWidth:self.source.text];
        self.source.frame = frame;
        
        CGRect frame2 = self.date.frame;
        frame2.origin.x = 20 * SCREEN_WIDTH + frame.size.width ;
        self.date.frame = frame2;
    }
}


@end
